<?php


namespace app\api\v1\graphQLModel;


use app\api\model\shortChainModel;
use GraphQL\Type\Definition\ResolveInfo;
use iflow\Swoole\GraphQL\Annotation\Type\lib\ArgsType;
use iflow\Swoole\GraphQL\Annotation\Type\lib\fieldType;
use iflow\Swoole\GraphQL\Annotation\Type\lib\resolveType;
use iflow\Swoole\GraphQL\Annotation\Type\TypeAnnotation;

#[TypeAnnotation('shortChain')]
class ShortChainGraphQL
{

    #[fieldType('createShort', 'string', '添加短链')]
    #[ArgsType([
        // url地址
        'url' => [
            'type' => 'nonNull->string'
        ]
    ])]
    #[resolveType(ShortChainGraphQL::class.'@shortResolveSave')]
    public string $createShortChain = '';

    #[fieldType('getShort', 'string', '获取短链真实地址')]
    #[ArgsType([
        // 短链Code
        'shortUrl' => [
            'type' => 'nonNull->string'
        ]
    ])]
    #[resolveType(ShortChainGraphQL::class.'@shortResolveGet')]
    public string $getShortChain = '';


    /**
     * 储存 短链
     * @param $rootValue
     * @param array $args
     * @param $context
     * @param ResolveInfo $info
     * @return string
     * @throws \Exception
     */
    public function shortResolveSave($rootValue, array $args, $context, ResolveInfo $info): string
    {
        $model = new shortChainModel();
        return $model -> saveShortUrl($args['url']);
    }

    /**
     * 获取短链
     * @param $rootValue
     * @param array $args
     * @param $context
     * @param ResolveInfo $info
     * @return string
     */
    public function shortResolveGet($rootValue, array $args, $context, ResolveInfo $info): string
    {
        $model = new shortChainModel();
        return $model -> getShortUrl($args['shortUrl']);
    }

}